home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1993 / Internet Info CD-ROM (Walnut Creek) (1993).iso / networking / ip / ka9q / osrc.arc / MISC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-19  |  3.6 KB  |  195 lines

  1. /* Miscellaneous machine independent utilities */
  2. #include <stdio.h>
  3. #include "global.h"
  4.  
  5. /* Convert hex-ascii to integer */
  6. int
  7. htoi(s)
  8. char *s;
  9. {
  10.     int i = 0;
  11.     char c;
  12.  
  13.     while((c = *s++) != '\0'){
  14.         if(c == 'x')
  15.             continue;    /* allow 0x notation */
  16.         if('0' <= c && c <= '9')
  17.             i = (i * 16) + (c - '0');
  18.         else if('a' <= c && c <= 'f')
  19.             i = (i * 16) + (c - 'a' + 10);
  20.         else if('A' <= c && c <= 'F')
  21.             i = (i * 16) + (c - 'A' + 10);
  22.         else
  23.             break;
  24.     }
  25.     return i;
  26. }
  27. /* replace terminating end of line marker(s) with null */
  28. void
  29. rip(s)
  30. register char *s;
  31. {
  32.     register char *cp;
  33.  
  34.     if((cp = strchr(s,'\r')) != NULLCHAR)
  35.         *cp = '\0';
  36.     if((cp = strchr(s,'\n')) != NULLCHAR)
  37.         *cp = '\0';
  38. }
  39. /* Routines not needed for Turbo 2.0, but available for older libraries */
  40. #ifdef    AZTEC
  41.  
  42. /* Copy a string to a malloc'ed buffer */
  43. char *
  44. strdup(s)
  45. const char *s;
  46. {
  47.     register char *out;
  48.     register int len;
  49.  
  50.     if(s == NULLCHAR)
  51.         return NULLCHAR;
  52.     len = strlen(s);
  53.     if((out = malloc(len+1)) == NULLCHAR)
  54.         return NULLCHAR;
  55.     /* This is probably a tad faster than strcpy, since we know the len */
  56.     memcpy(out,s,len);
  57.     out[len] = '\0';
  58.     return out;
  59. }
  60.  
  61. /* Case-insensitive string comparison */
  62. strnicmp(a,b,n)
  63. register char *a,*b;
  64. register int n;
  65. {
  66.     char a1,b1;
  67.  
  68.     while(n-- != 0 && (a1 = *a++) != '\0' && (b1 = *b++) != '\0'){
  69.         if(a1 == b1)
  70.             continue;    /* No need to convert */
  71.         a1 = tolower(a1);
  72.         b1 = tolower(b1);
  73.         if(a1 == b1)
  74.             continue;    /* NOW they match! */
  75.         if(a1 > b1)
  76.             return 1;
  77.         if(a1 < b1)
  78.             return -1;
  79.     }
  80.     return 0;
  81. }
  82.  
  83. char *
  84. strtok(s1,s2)
  85. char *s1;    /* Source string (first call) or NULL */
  86. #ifdef    __STDC__    /* Ugly kludge for aztec's declaration */
  87. const char *s2;    /* Delimiter string */
  88. #else
  89. char *s2;    /* Delimiter string */
  90. #endif
  91. {
  92.     static int isdelim();
  93.     static char *next;
  94.     register char *cp;
  95.     char *tmp;
  96.  
  97.     if(s2 == NULLCHAR)
  98.         return NULLCHAR;    /* Must give delimiter string */
  99.  
  100.     if(s1 != NULLCHAR)
  101.         next = s1;        /* First call */
  102.  
  103.     if(next == NULLCHAR)
  104.         return NULLCHAR;    /* No more */
  105.  
  106.     /* Find beginning of this token */
  107.     for(cp = next;*cp != '\0' && isdelim(*cp,s2);cp++)
  108.         ;
  109.  
  110.     if(*cp == '\0')
  111.         return NULLCHAR;    /* Trailing delimiters, no token */
  112.  
  113.     /* Save the beginning of this token, and find its end */
  114.     tmp = cp;
  115.     next = NULLCHAR;    /* In case we don't find another delim */
  116.     for(;*cp != '\0';cp++){
  117.         if(isdelim(*cp,s2)){
  118.             *cp = '\0';
  119.             next = cp + 1;    /* Next call will begin here */
  120.             break;
  121.         }
  122.     }
  123.     return tmp;
  124. }
  125. static int
  126. isdelim(c,delim)
  127. char c;
  128. register char *delim;
  129. {
  130.     char d;
  131.  
  132.     while((d = *delim++) != '\0'){
  133.         if(c == d)
  134.             return 1;
  135.     }
  136.     return 0;
  137. }
  138. #endif    /* AZTEC */
  139.  
  140. /* Host-network conversion routines, replaced on the 8086 with assembler */
  141. #ifndef    MSDOS
  142. /* Put a long in host order into a char array in network order */
  143. char *
  144. put32(cp,x)
  145. register char *cp;
  146. int32 x;
  147. {
  148.     *cp++ = x >> 24;
  149.     *cp++ = x >> 16;
  150.     *cp++ = x >> 8;
  151.     *cp++ = x;
  152.     return cp;
  153. }
  154. /* Put a short in host order into a char array in network order */
  155. char *
  156. put16(cp,x)
  157. register char *cp;
  158. int16 x;
  159. {
  160.     *cp++ = x >> 8;
  161.     *cp++ = x;
  162.  
  163.     return cp;
  164. }
  165. int16
  166. get16(cp)
  167. register char *cp;
  168. {
  169.     register int16 x;
  170.  
  171.     x = uchar(*cp++);
  172.     x <<= 8;
  173.     x |= uchar(*cp);
  174.     return x;
  175. }
  176. /* Machine-independent, alignment insensitive network-to-host long conversion */
  177. int32
  178. get32(cp)
  179. register char *cp;
  180. {
  181.     int32 rval;
  182.  
  183.     rval = uchar(*cp++);
  184.     rval <<= 8;
  185.     rval |= uchar(*cp++);
  186.     rval <<= 8;
  187.     rval |= uchar(*cp++);
  188.     rval <<= 8;
  189.     rval |= uchar(*cp);
  190.  
  191.     return rval;
  192. }
  193. #endif
  194.  
  195.